﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CourseArraging.Common;
using CourseArraging.Frm.bbxx;
using Stone.Library.Common;
using Stone.DataContracts;
using CourseArraging.Frm.jxgl;

namespace CourseArraging.Frm.jygl
{
    /// <summary>
    /// 版权所有
    /// 石世特@温州医学院 sst330381@gmail.com
    /// </summary> 
    public partial class FrmTecCur : FrmToolbarBase
    {
        #region 字段属性
        int pageSize = 0;     //每页显示行数
        int nMax = 0;         //总记录数
        int pageCount = 0;    //页数＝总记录数/每页显示行数
        int pageCurrent = 0;   //当前页号
        int nCurrent = 0;      //当前记录行

        TecArrangeEntity tecEntity;//记录当前教师实例

        private DataTable dtInfo = new DataTable();
        private static FrmTecCur instance;
        YhxxService.YhxxServiceClient yhxxdao = new YhxxService.YhxxServiceClient();
        #endregion

        #region 构造器
        public static FrmTecCur GetInstance()
        {
            if (instance == null || instance.IsDisposed)
            {
                instance = new FrmTecCur();
                instance.MdiParent = GlobalVar.frmMain;
            }
            instance.BringToFront();
            return instance;
        }
        public FrmTecCur()
        {
            InitializeComponent();
            this.bdnInfo.ItemClicked += new ToolStripItemClickedEventHandler(bdnInfo_ItemClicked);
            this.dgvInfo.CellClick += new DataGridViewCellEventHandler(dgvInfo_CellClick);
            this.uCkcb1.ClickEvent += new UControl.UCkcb.ClickEventHandle(uCkcb1_ClickEvent);  //mlh 2011-03-29 增加
            dtInfo = yhxxdao.GetTecinfo(null, null, null, null, null, null, null);
            this.uCkcb1.FadeTimeSet();
            InitDataSet();
        }


        #endregion

        #region 事件
        void uCkcb1_ClickEvent(string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                FrmLsap frm = new FrmLsap(new LsapEntity()
                {
                    lsap_jyxm = this.dgvInfo.CurrentRow.Cells["姓名"].Value.ToString(),
                    //lsap_kcsj = "6-" + multiColHeaderDgv1.Columns[e.ColumnIndex].HeaderText,
                    lsap_kcsj = str,
                    lsap_jybh = this.dgvInfo.CurrentRow.Cells["编号"].Value.ToString()
                });
                frm.lsapevent += new FrmLsap.lsapDelegate(frm_lsapevent);
                frm.ShowDialog();
            }
        }

        void frm_lsapevent()
        {
            DataGridViewCellEventArgs e = new DataGridViewCellEventArgs(1, dgvInfo.CurrentCell.RowIndex);
            dgvInfo_CellClick(null, e);
        }

        void dgvInfo_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            initBaseInfo();
            if (e.RowIndex < 0) return;
            tecEntity = yhxxdao.GetTecArrangeEntity(
                this.dgvInfo.Rows[e.RowIndex].Cells["教员流水"].Value.ToString());
            txt_name.Text = tecEntity.user_name;
            txt_spell.Text = tecEntity.user_spell;
            txt_sex.Text = tecEntity.user_sex;
            txt_grade.Text = tecEntity.user_grade;
            txt_gddh.Text = tecEntity.user_gddh;
            txt_phone.Text = tecEntity.user_phone;
            txt_address.Text = tecEntity.user_address;
            txt_dep.Text = tecEntity.user_depname;
            txt_bz.Text = tecEntity.user_bz;
            txt_course.Text = tecEntity.user_course;
            label_regedit.Text = tecEntity.user_regedit;

            string[] coursename = CommonHelper.GetSplitString(tecEntity.user_course);
            foreach (CourseEntity ce in tecEntity.course_info)
            {
                string[] coursetime = CommonHelper.GetSplitString(ce.course_time);
                for (int i = 0; i < coursetime.Length; i++)
                {
                    string[] tmpstring = coursetime[i].Split(new char[] { '-' });
                    int tmp = 1;
                    if (DateTime.Parse(tmpstring[1]) >= DateTime.Parse("18:00"))
                        tmp = 7;
                    else if (DateTime.Parse(tmpstring[1]) >= DateTime.Parse("12:00"))
                        tmp = 4;
                    uCkcb1.SetCellData(
                        int.Parse(tmpstring[0]), tmp, null, tmpstring[1] + "-" + tmpstring[2] + "-" + ce.course_name + "-" + ce.stu_name + "-" + ce.course_bz);
                }
            }

            uCkcb1.ClickEnable = true;
        }

        void bdnInfo_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Text == "上一页")
            {
                pageCurrent--;
                if (pageCurrent <= 0)
                {
                    MessageBox.Show("已经是第一页，请点击“下一页”查看！");
                    return;
                }
                else
                {
                    nCurrent = pageSize * (pageCurrent - 1);
                }

                LoadData();
            }
            if (e.ClickedItem.Text == "下一页")
            {
                pageCurrent++;
                if (pageCurrent > pageCount)
                {
                    MessageBox.Show("已经是最后一页，请点击“上一页”查看！");
                    return;
                }
                else
                {
                    nCurrent = pageSize * (pageCurrent - 1);
                }
                LoadData();
            }
        }

        #endregion

        #region 实现抽象方法

        public override void tsb_print_fun()
        {
            if (tecEntity != null)
            {
                //小马
                FrmTecKb frm = new FrmTecKb(tecEntity);
                frm.ShowDialog();
            }
        }

        public override void tsb_refresh_fun()
        {
            dtInfo = yhxxdao.GetTecinfo(null, null, null, null, null, null, null);
            InitDataSet();
        }

        public override void tsb_search_fun()
        {
            FrmTecZmCondi frm = FrmTecZmCondi.getInstance(FrmTecZmCondi.FrmMode.FRM_TEC_KCB);
            frm.FilterDTEvent += new FrmTecZmCondi.FilterDTEventHandler(frm_FilterDTEvent);
            frm.ShowDialog();
        }

        void frm_FilterDTEvent(DataTable dt)
        {
            dtInfo = dt;
            InitDataSet();
        }

        #endregion

        #region 方法

        private void InitDataSet()
        {
            pageSize = 10;      //设置页面行数
            nMax = dtInfo.Rows.Count;
            if (nMax == 0)
            {
                //MessageBox.Show("未找到符合条件的记录");
                dgvInfo.DataSource = null;
                dgvInfo.Refresh();
                return;
            }
            pageCount = (nMax / pageSize);    //计算出总页数

            if ((nMax % pageSize) > 0) pageCount++;

            pageCurrent = 1;    //当前页数从1开始
            nCurrent = 0;       //当前记录数从0开始

            LoadData();
        }

        private void LoadData()
        {
            int nStartPos = 0;   //当前页面开始记录行
            int nEndPos = 0;     //当前页面结束记录行

            DataTable dtTemp = dtInfo.Clone();   //克隆DataTable结构框架

            if (pageCurrent == pageCount)
                nEndPos = nMax;
            else
                nEndPos = pageSize * pageCurrent;

            nStartPos = nCurrent;

            lblPageCount.Text = pageCount.ToString();
            txtCurrentPage.Text = Convert.ToString(pageCurrent);

            //从元数据源复制记录行
            for (int i = nStartPos; i < nEndPos; i++)
            {
                dtTemp.ImportRow(dtInfo.Rows[i]);
                nCurrent++;
            }
            bdsInfo.DataSource = dtTemp;
            bdnInfo.BindingSource = bdsInfo;
            dgvInfo.DataSource = bdsInfo;
            dgvInfo.Columns["教员流水"].Visible = false;
            dgvInfo.Columns["分校"].Visible = false;
        }

        public void initBaseInfo()
        {
            txt_course.Text = string.Empty;
            //txt_identify.Text = string.Empty;
            txt_name.Text = string.Empty;
            txt_phone.Text = string.Empty;
            txt_spell.Text = string.Empty;
            txt_course.Text = string.Empty;
            txt_gddh.Text = string.Empty;
            txt_address.Text = string.Empty;
            txt_bz.Text = string.Empty;
            this.uCkcb1.setCaption("选择教师可以授课的时间");
            this.uCkcb1.initeDataGrid();
        }

        #endregion
    }
}
